package com.cyxu.moviedigest.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.cyxu.moviedigest.R;
import com.cyxu.moviedigest.data.AppConstant;

import org.json.JSONArray;

public class LoginActivity extends Activity {

    private EditText et_username;
    private EditText et_passwd;
    private String username;
    private String password;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        et_username = (EditText)findViewById(R.id.editText1);
        et_passwd = (EditText)findViewById(R.id.editText2);
        Button btn_login = (Button)findViewById(R.id.button1);
        TextView tv_register = (TextView)findViewById(R.id.tv_register);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptLogin();
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    public void attemptLogin() {

        username = et_username.getText().toString();
        password = et_passwd.getText().toString();
        String url = AppConstant.SERVER_URL + "/login?username=" + username + "&password=" + password;

        RequestQueue queue = Volley.newRequestQueue(this);

            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray jsonArray) {

                SharedPreferences sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("username", username);
                editor.putString("password", password);
                editor.putString("movie", jsonArray.toString());
                editor.apply();

                Toast.makeText(getBaseContext(), "Login success!", Toast.LENGTH_SHORT).show();
                toMain();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                if (volleyError.networkResponse != null) {
                    int status = volleyError.networkResponse.statusCode;
                    Log.e("Login_HTTP_RESPONSE", Integer.toString(status));
                }
                Toast.makeText(getBaseContext(), "Wrong username or password!", Toast.LENGTH_LONG).show();
            }
        });

        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
        queue.add(jsonArrayRequest);

    }

    public void toMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}



